package cn.itcast.controller;

import cn.itcast.domain.Standard;
import cn.itcast.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StandardController {

    @Autowired
    private StandardService standardService;

    /**
     * 添加的方法
     * @param standard
     * @return
     */
    @RequestMapping("/standard_save")
    public String save(Standard standard) {
        System.out.println(standard);
        standardService.save(standard);
        //重定向到页面 默认是转发
        return "redirect:/pages/base/standard.html";

    }

    /**
     * 分页查询的方法
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/standard_pageQuery")
    @ResponseBody
    public Map pageQuery(int page, int rows) {
        System.out.println("page=" + page + "rows=" + rows);
        Page<Standard> pageResult = standardService.pageQuery(page, rows);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total", pageResult.getTotalElements());
        hashMap.put("rows", pageResult.getContent());
        return hashMap;
    }

    /**
     * 根据删除方法
     * @param ids
     * @return
     */
    @RequestMapping("standard_deletebyId")
    public String deleteById(@RequestParam(value="ids")String ids) {
        String[] split = ids.split(",");
        standardService.batchDel(split);
        return "redirect:/pages/base/standard.html";
    }

    //查询所有收派标准方法
    @RequestMapping("standard_findAll")
    @ResponseBody
    public List findAll() {
        List<Standard> standards = standardService.courierfindAll();
        return standards;
    }
}